package com.cwt.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwt.common.constant.SysConstant;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.FileUtils;
import com.cwt.common.utils.QRCodeUtils;
import com.cwt.common.utils.StringUtils;
import com.cwt.service.domain.ShopOrder;
import com.cwt.service.service.ShopOrderService;
import com.cwt.service.mapper.ShopOrderMapper;
import com.google.zxing.WriterException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
* @author Acer
* @description 针对表【tb_shop_order(商品订单表)】的数据库操作Service实现
* @createDate 2023-08-09 17:18:36
*/
@Service
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderMapper, ShopOrder>
    implements ShopOrderService{

    @Override
    public AjaxResult qrcode(ShopOrder order) {
        String orderNo = StringUtils.defaultString(order.getOrderNumber(), StringUtils.getOrderID());

        //首先，因为这儿是非Controller，所以，通过RequestContextHolder获取HttpServletRequest；
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        //然后，拼凑订单支付url的一部分：“127.0.0.1:8083”;
        String address = "localhost" + ":" + request.getLocalPort() + request.getContextPath();
        //然后，完整拼凑订单支付url：“http://127.0.0.1:8083//pay?orderNo=订单号”;
        //这个就是将要写到二维码中的信息；其实，也是后面的【前台：支付订单】接口的，附带了orderNo参数的完整url
        String payUrl = "http://" + address + "/pay?orderNo=" + orderNo;

        //然后，调用我们在QRCodeGenerator工具类中编写的，生成二维码的方法；
        payUrl = "el-table添加了selection-change事件监听，当选择行发生变化时，会触发handleSelectionChange方法。在该方法中，将选中的行数据保存在selectedRows数组或对象中。";
        try {
            QRCodeUtils.generateQRCode(payUrl, 118, 118, SysConstant.ORDER_UPLOAD_DIR + orderNo + ".png");
        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }

        //获取二维码图片的访问地址；（PS：仅仅是访问地址，而是访问地址）
        String pngAddress = "http://" + address + "/images/" + orderNo + ".png";
        //然后，把这个二维码图片的访问地址返回；
        return AjaxResult.success(pngAddress);
    }

    @Override
    public AjaxResult removeQrcode(String qrcodeName) {
        return AjaxResult.success(FileUtils.deleteFile(SysConstant.ORDER_UPLOAD_DIR + qrcodeName.replaceAll("\"", "")));
    }

    @Override
    public AjaxResult getQrcode(String orderNumber) {
        //首先，因为这儿是非Controller，所以，通过RequestContextHolder获取HttpServletRequest；
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        //然后，拼凑订单支付url的一部分：“127.0.0.1:8083”;
        String address = "localhost" + ":" + request.getLocalPort();
        return AjaxResult.success("http://" + address + "/images/" + orderNumber.replaceAll("\"", "") + ".png");
    }
}




